<?php
class MaterialReceiptSearch extends CFormModel {
	public $receiptDateMin;
	public $receiptDateMax;
	public $token;
	public $handler;
	public $merchantId;
	public $projectId;
	public $materialModelId;

	public function rules() {
		return array(
			array(
				'projectId, materialModelId, merchantId',
				'numerical',
				'integerOnly' => true,
			),
			array(
				'receiptDateMin, receiptDateMax',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'token, handler',
				'length',
				'max' => 255
			),
		);
	}

	public function attributeLabels() {
		return array(
			'token' => '单据号',
			'projectId' => '项目',
			'handler' => '经手人',
			'merchantId' => '商家',
			'receiptDateMin' => '单据时间',
			'materialModelId' => '包含物品'
		);
	}

	/**
	 * @return CDbCriteria
	 */
	public function createCriteria($fast = false) {
		$criteria = new CDbCriteria;
		if (!$fast) {
			$criteria->with = array(
				'itemCount',
				'project',
				'warehouseManager',
				'merchant',
				'createUser.employee',
			);
			if ($this->materialModelId) {
				$criteria->with[] = 'items';
				$criteria->together = true;
			}
		}

		if ($this->receiptDateMin) {
			$criteria->compare('receiptDate', '>=' . $this->receiptDateMin);
		}
		if ($this->receiptDateMax) {
			$criteria->compare('receiptDate', '<=' . $this->receiptDateMax);
		}
		$criteria->compare('handler', $this->handler);
		$criteria->compare('merchantId', $this->merchantId);
		$criteria->compare('projectId', $this->projectId);
		$criteria->compare('token', $this->token);

		if ($this->materialModelId) {
			$criteria->compare('materialModelId', $this->materialModelId);
		}
		$criteria->order = 'receiptDate DESC';

		return $criteria;
	}
}
